<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Championship;
use App\Models\CityList;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ChampionshipController extends Controller
{
    use HasResourceActions;
    private $name = '锦标赛';

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.list'))
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.detail'))
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Championship);
        $grid->model()->where('status', '>', 0)->orderBy('id', 'desc');

        $grid->id('ID')->sortable();
        $grid->cityInfo()->name('城市');
        $grid->title('标题');
        $grid->game_datetime('比赛开始时间');
        $grid->end_game_datetime('比赛结束时间');
        $grid->address('地点');
        $grid->signup_datetime('报名截止');
        $grid->signup_price('报名费');
        $grid->status('状态')->display(function ($status) {
            return Championship::status($status);
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $championship = Championship::findOrFail($id);
        $show = new Show($championship);

        $show->id('ID');
        $show->organizer('组织者')->unescape()->as(function ($val) use ($championship) {
            return $championship->organizerUser->name . '-' . $championship->organizerUser->tele;
        });
        $show->title('标题');
        $show->game_datetime('比赛开始时间');
        $show->end_game_datetime('比赛结束时间');
        $show->address('比赛地点');
        $show->signup_datetime('报名截止');
        $show->signup_price('报名费');
        $show->remark('简介');
        $show->status('状态')->unescape()->as(function ($status) {
            return Championship::status($status);
        });
        $show->prize('奖品')->unescape()->as(function ($prize) {
            return $prize;
        });
        $show->desc('描述')->unescape()->as(function ($desc) {
            return $desc;
        });

        $show->signup('报名者', function ($signup) {
            $signup->setResource('/admin/Championship');
            $signup->user()->name('名称');
            $signup->user()->head_img('头像')->image('', 50, 50);

            $signup->disableExport();
            $signup->disableRowSelector();
            $signup->actions(function ($actions) {
                $actions->disableView();
            });
        });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Championship);
        $cityList = CityList::getCityList();

        $form->select('city', '城市')->options($cityList)->rules('required');
        $form->text('title', '标题');
        $form->datetime('game_datetime', '比赛开始时间')->default(date('Y-m-d H:i:s'));
        $form->datetime('end_game_datetime', '比赛结束时间')->default(date('Y-m-d H:i:s'));
        $form->text('address', '比赛地点');
        $form->datetime('signup_datetime', '报名截止')->default(date('Y-m-d H:i:s'));
        $form->text('signup_price', '报名费');
        $form->text('remark', '赛事简介');
        $form->editor('prize', '奖品');
        $form->editor('desc', '补充说明');
        $form->editor('result', '赛事结果');

        $para = request()->route()->parameters();
        if (count($para) > 0) {
            $id = $para['champion'];
            $championship = Championship::find($id);
            if ($championship->status == 10) {
                $form->tools(function (Form\Tools $tools) use ($id) {
                    $url = route('champion.confirm', ['id' => $id]);
                    $tools->add('<div class="btn-group pull-right" style="margin-right: 5px"><a class="btn btn-sm btn-danger" href = "' . $url . '"><i class="fa fa-trash"></i>确认</a></div>');
                });
            } else if ($championship->status == 20) {
                $form->tools(function (Form\Tools $tools) use ($id) {
                    $url = route('champion.close', ['id' => $id]);
                    $tools->add('<div class="btn-group pull-right" style="margin-right: 5px"><a class="btn btn-sm btn-danger" href = "' . $url . '"><i class="fa fa-trash"></i>结案</a></div>');
                });
            }

        }

        return $form;
    }

    public function confirm($id)
    {
        $championship = Championship::find($id);
        if ($championship->status != 10) {
            admin_toastr('当前赛事已经确认过了', 'error');
            return back();
        }
        $championship->status = 20;
        if ($championship->save()) {
            admin_toastr('确认成功', 'success');
        } else {
            admin_toastr('确认失败', 'error');
        }

        $url = route('champion.index');
        return redirect($url);
    }

    public function close($id)
    {
        $championship = Championship::find($id);
        if ($championship->status != 20) {
            admin_toastr('当前赛事不是比赛中，不能结案', 'error');
            return back();
        }
        $championship->status = 30;
        if ($championship->save()) {
            admin_toastr('确认成功', 'success');
        } else {
            admin_toastr('确认失败', 'error');
        }

        $url = route('champion.index');
        return redirect($url);
    }
}
